package org.flh.book.shop.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 描述：返回体
 *
 * @author 花好月圆
 * @date 2020/11/19 11:17
 */
@Data
@ApiModel
public class RestResponse<T> {

    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private String status;

    /**
     * 含义
     */
    @ApiModelProperty("描述")
    private String message;

    /**
     * 返回数据
     */
    @ApiModelProperty("返回数据")
    private T data;

    /**
     * 成功
     */
    private static final String SUCCESS = "0000";

    private static final String SUCCESS_MESSAGE = "调用成功";

    /**
     * 失败
     */
    private static final String FAIL = "9999";

    private static final String FAIL_MESSAGE = "调用失败";

    private RestResponse(String status, String message) {
        this.status = status;
        this.message = message;
    }

    private RestResponse(String status, String message, T data){
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public static RestResponse success() {
        return new RestResponse(SUCCESS, SUCCESS_MESSAGE);
    }

    public static <T> RestResponse<T> success(T data) {
        return new RestResponse<>(SUCCESS, SUCCESS_MESSAGE, data);
    }

    public static RestResponse fail(String message) {
        return new RestResponse(FAIL, message);
    }

    public static <T> RestResponse fail(String message, T data) {
        return new RestResponse<>(FAIL, message, data);
    }

}
